// pages/address/address.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:null,
    region:"",
    detailaddress:'',
    username:"",
    tel:"",
    arrDetail:[], //地址集合
    index:null
  },
  //返回按钮
  onClickLeft(){
    wx.navigateTo({
      url:'/pages/order/order'
    })
  },
  //保存地址
  removeAddress(e){
    console.log(e)
    const list =wx.getStorageSync('userAddress')
    console.log(list)
    let {username,tel,region,detailaddress} = this.data
    const index = list.findIndex(item=>{
      return item.username ==username&&item.tel == tel&&item.region == region&&item.detailaddress == detailaddress
    })
    console.log(index)
    list.splice(index,1)
    
    wx.showModal({
      title: '确定删除吗',
      success(res){
        if(res.confirm){
          wx.setStorageSync("userAddress",list)
          wx.navigateTo({
            url: '/pages/saveAddress/saveAddress'
          })
        }else{
          wx.showToast({
            title: '未删除',
          })
        }
        
      }

    })
    
  },



  savaAddress(){
    let {region,detailaddress,username,tel,index} = this.data

    console.log(this.data.region)
    if(this.data.region == undefined) return Toast('城市不能为空');
    if(this.data.username  == undefined) return Toast('收货人不能为空');
    if(this.data.tel == undefined) return Toast('手机号码不能为空');
    if(this.data.detailaddress== undefined) return Toast('街道信息不能为空');
    console.log(this.data.index+"-----"+this.data.tel)
    if(this.data.index&&this.data.tel!="" || this.data.name == '1'){
      const arr = wx.getStorageSync('userAddress')
      let index = this.data.index;
      let obj = {
        username:this.data.username,
        tel:this.data.tel,
        region : this.data.region,
        detailaddress : this.data.detailaddress
      }
      arr.splice(index,1,obj)
      console.log(arr)
      wx.setStorageSync('userAddress', arr)
      wx.navigateTo({
        url: `/pages/saveAddress/saveAddress`
      })
    }else{
    
    wx.navigateTo({
      url: `/pages/saveAddress/saveAddress?region=${region}&detailaddress=${detailaddress}&username=${username}&tel=${tel}$index=${index}`
    })
    //本地存一份地址
    //将用户收获地址保存到本地
    
    let user = [{
      username,
      tel,
      region,
      detailaddress
    }]
    this.setData({
      arrDetail:[...this.data.arrDetail,...user]
    })
    wx.setStorageSync('userAddress',this.data.arrDetail)
  }
  },

  //双向绑定省市县
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },






  getAddress(){

    wx.chooseAddress({
      success(res){
        console.log(res)
        const {provinceName,cityName,countyName,detailInfo,telNumber} = res;
        console.log(telNumber)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.name == '1'){
      //说明是从订单也跳转过来的
      let {username,tel,region,detailaddress,name,index} = options
      console.log(index+"woshi")
      
      let arr = wx.getStorageSync('userAddress')
      this.setData({
        username,tel,region,detailaddress,name
      })
      this.setData({
        arrDetail:arr
      })




    }
    if(options.name == ''){
      console.log("-----",options)
    
      // this.setData({
      //   username:'',
      //   tel:'',
      //   region:"",
      //   detailaddress:''
      // })
    }

    let arr = wx.getStorageSync('userAddress')
    console.log(arr) 
    let {username,tel,region,detailaddress,index} = options
    console.log(index)
    this.setData({
      index,
      username,
      tel,
      region,
      detailaddress
    })
  
    
    this.setData({
      arrDetail:arr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})